<!-- Author: Moe Kyaw Thu Aung --> 
<!-- Last Modified: 11 Sep 2012 -->
<?php
	include("../confs/config.php");

	$id = $_GET['id'];

	if(!$id) { header("location: list.php"); }
	
	$conn = mysql_connect($DB_HOST, $DB_USER, $DB_PASSWORD);
	mysql_select_db($DB_NAME, $conn);
	
	$result = mysql_query("SELECT * FROM items WHERE id=$id");
	$row = mysql_fetch_assoc($result);
	
	if(file_exists("../covers/".$row['cover']) && !is_dir("../covers/".$row['cover'])) {
		$cover = "../covers/".$row['cover'];
	} else {
		$cover = "../covers/no.jpg";
	}
?>

<html>
	<head>
		<title>My Book Store : Administration</title>
		<link rel="stylesheet" href="../css/admin.css" />

		<script src="../js/jquery.js"></script>
		<script src="../js/jquery.validate.js"></script>
		<script>
			$(document).ready(function() {
				$("form").validate({
					rules: {
						name: {
							required: true,
							minlength: 4
						},
						price: {
							required: true,
							number: true
						},
						qty: {
							required: true,
							number: true
						},
					}
				});

				$("#change-cover").click(function () {
					$('input[type=file]').toggle();
					if($(this).html() == "Change Cover") {
						
						$(this).html("Cancel");
						$('img').hide();
						$('input[type=file]').focus();
						$('#changed-cover').val('1');
						
					} else {
						
						$(this).html("Change Cover");
						$('input[type=file]').val('');
						$('img').show();
						$('#changed-cover').val('0');
						
					}
				});
			});
		</script>
	</head>
	<body>
		<div id="wrapper">
			<div id="head">
				<h1>My Book Store : Edit Item</h1>
				<a href="actions.php?do=logout">Logout</a>
			</div>

			<div id="new-form">
				<form action="actions.php?do=update&id=<?php echo $id ?>" method="post" enctype="multipart/form-data">
					<table>
						<tr>
							<td>Name</td>
							<td><input type="text" name="name" value="<?php echo $row['name'] ?>" /></td>
						</tr>
						<tr>
							<td valign='top'>Description</td>
							<td><textarea name="des"><?php echo $row['des'] ?></textarea> </td>
						</tr>
						<tr>
							<td>Price</td>
							<td><input type="text" name="price" value="<?php echo $row['price'] ?>" /> </td>
						</tr>
						<tr>
							<td>Quantity</td>
							<td><input type="text" name="qty" value="<?php echo $row['qty'] ?>" /></td>
						</tr>
						<tr>
							<td valign='top'><br />Cover</td>
							<td>
								<br /><img src="<?php echo $cover ?>" width='120' height='144' />
								<input type="file" name="cover" style="display: none" />
								<br />
								<span style="display: none">Choose cover (or) </span>
								<a href="#" id="change-cover">Change Cover</a>
								<br /><br />
							</td>
						</tr>
						<tr>
							<td></td>
							<td>
								<input type="submit" value="Update &raquo;" />
								&nbsp; <a href="list.php">Back</a>
							</td>
						</tr>
					</table>
					
					<input id='changed-cover' type='hidden' name='changed_cover' value='0' />
					
				</form>
			</div>
			
			<br /><br /><br />
			
			<div id="foot">
				&copy; OneStop JCU.
			</div>

		</div>
	</body>
</html>
